/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

 /*
  * This file was autogenerated by irt_syscalls.py
  */

#include <elf.h>
#include <irt_syscalls.h>
#include <stdlib.h>


// Each instance of LIBC will have it's own global copy of the irt syscalls.
// When using the dyanmic loader, the loader's irt pointers will need to
// get updated seperately.

_irt_syscalls_t g_nacl_irt_syscalls_data;
_irt_syscalls_t* g_nacl_irt_syscalls_ptr = &g_nacl_irt_syscalls_data;

static int not_implemented() {
  return (38 /* ENOSYS */);
}


static void IRT_INIT_NACL_IRT_BASIC(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_basic funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_BASIC_v0_1, &funcs, sizeof(struct nacl_irt_basic))
        == sizeof(struct nacl_irt_basic)) break;
  } while (0);
  irt->nacl_irt_exit = funcs.exit;
  irt->nacl_irt_gettod = funcs.gettod;
  irt->nacl_irt_clock = funcs.clock;
  irt->nacl_irt_nanosleep = funcs.nanosleep;
  irt->nacl_irt_sched_yield = funcs.sched_yield;
  irt->nacl_irt_sysconf = funcs.sysconf;
}

static void IRT_INIT_NACL_IRT_BLOCKHOOK(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_blockhook funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_BLOCKHOOK_v0_1, &funcs, sizeof(struct nacl_irt_blockhook))
        == sizeof(struct nacl_irt_blockhook)) break;
  } while (0);
  irt->nacl_irt_register_block_hooks = funcs.register_block_hooks;
}

static void IRT_INIT_NACL_IRT_CLOCK(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_clock funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_CLOCK_v0_1, &funcs, sizeof(struct nacl_irt_clock))
        == sizeof(struct nacl_irt_clock)) break;
  } while (0);
  irt->nacl_irt_clock_getres = funcs.clock_getres;
  irt->nacl_irt_clock_gettime = funcs.clock_gettime;
}

static void IRT_INIT_NACL_IRT_CODE_DATA_ALLOC(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_code_data_alloc funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_CODE_DATA_ALLOC_v0_1, &funcs, sizeof(struct nacl_irt_code_data_alloc))
        == sizeof(struct nacl_irt_code_data_alloc)) break;
  } while (0);
  irt->nacl_irt_allocate_code_data = funcs.allocate_code_data;
}

static void IRT_INIT_NACL_IRT_COND(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_cond funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_COND_v0_1, &funcs, sizeof(struct nacl_irt_cond))
        == sizeof(struct nacl_irt_cond)) break;
  } while (0);
  irt->nacl_irt_cond_create = funcs.cond_create;
  irt->nacl_irt_cond_destroy = funcs.cond_destroy;
  irt->nacl_irt_cond_signal = funcs.cond_signal;
  irt->nacl_irt_cond_broadcast = funcs.cond_broadcast;
  irt->nacl_irt_cond_wait = funcs.cond_wait;
  irt->nacl_irt_cond_timed_wait_abs = funcs.cond_timed_wait_abs;
}

static void IRT_INIT_NACL_IRT_DEV_FDIO(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_fdio funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_FDIO_v0_3, &funcs, sizeof(struct nacl_irt_dev_fdio))
        == sizeof(struct nacl_irt_dev_fdio)) break;
    if (query(NACL_IRT_DEV_FDIO_v0_2, &funcs, sizeof(struct nacl_irt_dev_fdio_v0_2))
        == sizeof(struct nacl_irt_dev_fdio_v0_2)) break;
    if (query(NACL_IRT_DEV_FDIO_v0_1, &funcs, sizeof(struct nacl_irt_fdio))
        == sizeof(struct nacl_irt_fdio)) break;
  } while (0);
  irt->nacl_irt_close = funcs.close;
  irt->nacl_irt_dup = funcs.dup;
  irt->nacl_irt_dup2 = funcs.dup2;
  irt->nacl_irt_read = funcs.read;
  irt->nacl_irt_write = funcs.write;
  irt->nacl_irt_seek = funcs.seek;
  irt->nacl_irt_fstat = funcs.fstat;
  irt->nacl_irt_getdents = funcs.getdents;
  irt->nacl_irt_fchdir = funcs.fchdir;
  irt->nacl_irt_fchmod = funcs.fchmod;
  irt->nacl_irt_fsync = funcs.fsync;
  irt->nacl_irt_fdatasync = funcs.fdatasync;
  irt->nacl_irt_ftruncate = funcs.ftruncate;
  irt->nacl_irt_isatty = funcs.isatty;
}

static void IRT_INIT_NACL_IRT_DEV_FILENAME(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_filename funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_FILENAME_v0_3, &funcs, sizeof(struct nacl_irt_dev_filename))
        == sizeof(struct nacl_irt_dev_filename)) break;
    if (query(NACL_IRT_DEV_FILENAME_v0_2, &funcs, sizeof(struct nacl_irt_dev_filename_v0_2))
        == sizeof(struct nacl_irt_dev_filename_v0_2)) break;
  } while (0);
  irt->nacl_irt_open = funcs.open;
  irt->nacl_irt_stat = funcs.stat;
  irt->nacl_irt_mkdir = funcs.mkdir;
  irt->nacl_irt_rmdir = funcs.rmdir;
  irt->nacl_irt_chdir = funcs.chdir;
  irt->nacl_irt_getcwd = funcs.getcwd;
  irt->nacl_irt_unlink = funcs.unlink;
  irt->nacl_irt_truncate = funcs.truncate;
  irt->nacl_irt_lstat = funcs.lstat;
  irt->nacl_irt_link = funcs.link;
  irt->nacl_irt_rename = funcs.rename;
  irt->nacl_irt_symlink = funcs.symlink;
  irt->nacl_irt_chmod = funcs.chmod;
  irt->nacl_irt_access = funcs.access;
  irt->nacl_irt_readlink = funcs.readlink;
  irt->nacl_irt_utimes = funcs.utimes;
}

static void IRT_INIT_NACL_IRT_DEV_GETPID(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_getpid funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_GETPID_v0_1, &funcs, sizeof(struct nacl_irt_dev_getpid))
        == sizeof(struct nacl_irt_dev_getpid)) break;
  } while (0);
  irt->nacl_irt_getpid = funcs.getpid;
}

static void IRT_INIT_NACL_IRT_DEV_LIST_MAPPINGS(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_list_mappings funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_LIST_MAPPINGS_v0_1, &funcs, sizeof(struct nacl_irt_dev_list_mappings))
        == sizeof(struct nacl_irt_dev_list_mappings)) break;
  } while (0);
  irt->nacl_irt_list_mappings = funcs.list_mappings;
}

static void IRT_INIT_NACL_IRT_DEV_POLL(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_poll funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_POLL_v0_1, &funcs, sizeof(struct nacl_irt_dev_poll))
        == sizeof(struct nacl_irt_dev_poll)) break;
  } while (0);
  irt->nacl_irt_poll = funcs.poll;
  irt->nacl_irt_select = funcs.select;
  irt->nacl_irt_epoll_create = funcs.epoll_create;
  irt->nacl_irt_epoll_ctl = funcs.epoll_ctl;
  irt->nacl_irt_epoll_wait = funcs.epoll_wait;
}

static void IRT_INIT_NACL_IRT_DEV_SOCKET(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dev_socket funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DEV_SOCKET_v0_1, &funcs, sizeof(struct nacl_irt_dev_socket))
        == sizeof(struct nacl_irt_dev_socket)) break;
  } while (0);
  irt->nacl_irt_accept = funcs.accept;
  irt->nacl_irt_bind = funcs.bind;
  irt->nacl_irt_connect = funcs.connect;
  irt->nacl_irt_getpeername = funcs.getpeername;
  irt->nacl_irt_getsockname = funcs.getsockname;
  irt->nacl_irt_getsockopt = funcs.getsockopt;
  irt->nacl_irt_listen = funcs.listen;
  irt->nacl_irt_pipe = funcs.pipe;
  irt->nacl_irt_recvfrom = funcs.recvfrom;
  irt->nacl_irt_sendto = funcs.sendto;
  irt->nacl_irt_setsockopt = funcs.setsockopt;
  irt->nacl_irt_socket = funcs.socket;
  irt->nacl_irt_socketpair = funcs.socketpair;
  irt->nacl_irt_shutdown = funcs.shutdown;
}

static void IRT_INIT_NACL_IRT_DYNCODE(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_dyncode funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_DYNCODE_v0_1, &funcs, sizeof(struct nacl_irt_dyncode))
        == sizeof(struct nacl_irt_dyncode)) break;
  } while (0);
  irt->nacl_irt_dyncode_create = funcs.dyncode_create;
  irt->nacl_irt_dyncode_modify = funcs.dyncode_modify;
  irt->nacl_irt_dyncode_delete = funcs.dyncode_delete;
}

static void IRT_INIT_NACL_IRT_EXCEPTION_HANDLING(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_exception_handling funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_EXCEPTION_HANDLING_v0_1, &funcs, sizeof(struct nacl_irt_exception_handling))
        == sizeof(struct nacl_irt_exception_handling)) break;
  } while (0);
  irt->nacl_irt_exception_handler = funcs.exception_handler;
  irt->nacl_irt_exception_stack = funcs.exception_stack;
  irt->nacl_irt_exception_clear_flag = funcs.exception_clear_flag;
}

static void IRT_INIT_NACL_IRT_FILENAME(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_filename funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_FILENAME_v0_1, &funcs, sizeof(struct nacl_irt_filename))
        == sizeof(struct nacl_irt_filename)) break;
  } while (0);
  irt->nacl_irt_open = funcs.open;
  irt->nacl_irt_stat = funcs.stat;
}

static void IRT_INIT_NACL_IRT_FUTEX(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_futex funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_FUTEX_v0_1, &funcs, sizeof(struct nacl_irt_futex))
        == sizeof(struct nacl_irt_futex)) break;
  } while (0);
  irt->nacl_irt_futex_wait_abs = funcs.futex_wait_abs;
  irt->nacl_irt_futex_wake = funcs.futex_wake;
}

static void IRT_INIT_NACL_IRT_MEMORY(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_memory funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_MEMORY_v0_3, &funcs, sizeof(struct nacl_irt_memory))
        == sizeof(struct nacl_irt_memory)) break;
    if (query(NACL_IRT_MEMORY_v0_2, &funcs, sizeof(struct nacl_irt_memory_v0_2))
        == sizeof(struct nacl_irt_memory_v0_2)) break;
    if (query(NACL_IRT_MEMORY_v0_1, &funcs, sizeof(struct nacl_irt_memory_v0_1))
        == sizeof(struct nacl_irt_memory_v0_1)) break;
  } while (0);
  irt->nacl_irt_mmap = funcs.mmap;
  irt->nacl_irt_munmap = funcs.munmap;
  irt->nacl_irt_mprotect = funcs.mprotect;
}

static void IRT_INIT_NACL_IRT_MUTEX(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_mutex funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_MUTEX_v0_1, &funcs, sizeof(struct nacl_irt_mutex))
        == sizeof(struct nacl_irt_mutex)) break;
  } while (0);
  irt->nacl_irt_mutex_create = funcs.mutex_create;
  irt->nacl_irt_mutex_destroy = funcs.mutex_destroy;
  irt->nacl_irt_mutex_lock = funcs.mutex_lock;
  irt->nacl_irt_mutex_unlock = funcs.mutex_unlock;
  irt->nacl_irt_mutex_trylock = funcs.mutex_trylock;
}

static void IRT_INIT_NACL_IRT_RANDOM(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_random funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_RANDOM_v0_1, &funcs, sizeof(struct nacl_irt_random))
        == sizeof(struct nacl_irt_random)) break;
  } while (0);
  irt->nacl_irt_get_random_bytes = funcs.get_random_bytes;
}

static void IRT_INIT_NACL_IRT_RESOURCE_OPEN(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_resource_open funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_RESOURCE_OPEN_v0_1, &funcs, sizeof(struct nacl_irt_resource_open))
        == sizeof(struct nacl_irt_resource_open)) break;
  } while (0);
  irt->nacl_irt_open_resource = funcs.open_resource;
}

static void IRT_INIT_NACL_IRT_SEM(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_sem funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_SEM_v0_1, &funcs, sizeof(struct nacl_irt_sem))
        == sizeof(struct nacl_irt_sem)) break;
  } while (0);
  irt->nacl_irt_sem_create = funcs.sem_create;
  irt->nacl_irt_sem_destroy = funcs.sem_destroy;
  irt->nacl_irt_sem_post = funcs.sem_post;
  irt->nacl_irt_sem_wait = funcs.sem_wait;
}

static void IRT_INIT_NACL_IRT_THREAD(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_thread funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_THREAD_v0_1, &funcs, sizeof(struct nacl_irt_thread))
        == sizeof(struct nacl_irt_thread)) break;
  } while (0);
  irt->nacl_irt_thread_create = funcs.thread_create;
  irt->nacl_irt_thread_exit = funcs.thread_exit;
  irt->nacl_irt_thread_nice = funcs.thread_nice;
}

static void IRT_INIT_NACL_IRT_TLS(_irt_syscalls_t* irt) {
  TYPE_nacl_irt_query query = irt->nacl_irt_query;
  struct nacl_irt_tls funcs;
  memset(&funcs, 0, sizeof(funcs));
  do {
    if (query(NACL_IRT_TLS_v0_1, &funcs, sizeof(struct nacl_irt_tls))
        == sizeof(struct nacl_irt_tls)) break;
  } while (0);
  irt->nacl_irt_tls_init = funcs.tls_init;
  irt->nacl_irt_tls_get = funcs.tls_get;
}


void __init_irt_table(TYPE_nacl_irt_query query) {
  _irt_syscalls_t* irt = g_nacl_irt_syscalls_ptr;

  if ((irt == NULL) || (query == NULL))
    return;

  memset(irt, 0, sizeof(_irt_syscalls_t));
  irt->nacl_irt_query = query;

  IRT_INIT_NACL_IRT_BASIC(irt);
  IRT_INIT_NACL_IRT_BLOCKHOOK(irt);
  IRT_INIT_NACL_IRT_CLOCK(irt);
  IRT_INIT_NACL_IRT_CODE_DATA_ALLOC(irt);
  IRT_INIT_NACL_IRT_COND(irt);
  IRT_INIT_NACL_IRT_DEV_FDIO(irt);
  IRT_INIT_NACL_IRT_DEV_FILENAME(irt);
  IRT_INIT_NACL_IRT_DEV_GETPID(irt);
  IRT_INIT_NACL_IRT_DEV_LIST_MAPPINGS(irt);
  IRT_INIT_NACL_IRT_DEV_POLL(irt);
  IRT_INIT_NACL_IRT_DEV_SOCKET(irt);
  IRT_INIT_NACL_IRT_DYNCODE(irt);
  IRT_INIT_NACL_IRT_EXCEPTION_HANDLING(irt);
  IRT_INIT_NACL_IRT_FILENAME(irt);
  IRT_INIT_NACL_IRT_FUTEX(irt);
  IRT_INIT_NACL_IRT_MEMORY(irt);
  IRT_INIT_NACL_IRT_MUTEX(irt);
  IRT_INIT_NACL_IRT_RANDOM(irt);
  IRT_INIT_NACL_IRT_RESOURCE_OPEN(irt);
  IRT_INIT_NACL_IRT_SEM(irt);
  IRT_INIT_NACL_IRT_THREAD(irt);
  IRT_INIT_NACL_IRT_TLS(irt);
}

#ifndef AT_NULL
#define AT_NULL 0
#endif

#ifndef AT_SYSINFO
#define AT_SYSINFO 32
#endif

void __init_irt_from_auxv (uintptr_t *auxv) {
  for (; *auxv != AT_NULL; auxv += 2) {
    // Get the IRT query pointer, and replace it with a pointer to the
    // IRT syscall table.
    if (*auxv == AT_SYSINFO) {
      __nacl_irt_query = (size_t (*)(const char *, void*, size_t)) auxv[1];
    }
  }
  // We will just crash in __init_irt_table due to NULL pointer access
  // if we could not find irt->nacl_irt_query. This should not happen.
  __init_irt_table(__nacl_irt_query);
}
